package cate.game.gm.sub;

import cate.common.table.pay.PayItemRow;
import cate.game.GameBody;
import cate.game.pay.PayDeliverParam;
import cate.game.role.Role;
import cate.game.util.GameKit;
import cp.solution.business.gm.*;
import cp.solution.business.gm.dto.ActorNoteCmd;
import cp.solution.business.gm.dto.ActorPayCmd;
import cp.solution.business.pay.PayRecordEntity;
import cp.solution.business.pay.dto.PayDeliverResult;
import cp.solution.define.PayOrderType;
import easy.java.struct.ref.StringRef;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GmActorRun {

    private static void syncData(ActorLog cmd, Role role) {
        cmd.setGameId(role.getBase().gameId);
        cmd.setGroupCode(role.getGame().config.gs.getGroupCode());
        cmd.setSrvName(role.getGame().config.gs.getName());
        cmd.setRoleName(role.getBase().name);
        cmd.setOpenId(role.getBase().openId);
    }

    /** ConsoleServer发起的设置托号de请求 */
    public static ActorSetLog set(GameBody game, ActorSetLog cmd) {
        log.info("收到ConsoleServer发起的设置托号de请求：{}", cmd);
        Role role = game.role.getRole(cmd.getRoleId());
        syncData(cmd, role);
        role.getBase().actor.enabled = cmd.isEnabled();
//        role.getGame().notice.message(role, "ActorSet="+cmd.isEnabled());
        cmd.setExeCode(0);
        cmd.setExeMsg("执行成功");
        return cmd;
    }


    /** ConsoleServer发起的托号支付请求 */
    public static ActorPayLog pay(GameBody game, ActorPayCmd cmd) {
        log.info("收到ConsoleServer发起的托号支付请求：{}", cmd);
        Role role = game.role.getRole(cmd.getRoleId());
        syncData(cmd, role);

        if(!role.getBase().actor.enabled) {
            cmd.setExeCode(403);
            cmd.setExeMsg("actor.enabled is false");
            return cmd;
        }

        PayItemRow itemRow = role.getGame().table.pay.item.get(cmd.getPayItemTid());
        if(itemRow == null) {
            cmd.setExeCode(404);
            cmd.setExeMsg("tid="+cmd.getPayItemTid()+" pay-item null");
            return cmd;
        }
        cmd.setPayItemName(itemRow.name);
        cmd.setPayItemNameCn(itemRow.nameCn);

        if(ActorPayCmd.CMD_TYPE_QUERY == cmd.getCmdType()) {
            // 如果console只是来查询一下数据的，那么到此为止了
            return cmd;
        }

        PayRecordEntity record = new PayRecordEntity();
        record.setOrderType(PayOrderType.INTERNAL);
        record.setUid(GameKit.buildLong());
        record.setPayCents(itemRow.price);
        record.setPayTime(cmd.getExeTime());
        record.setAccountId(role.getBase().accountId);
        record.setRoleName(role.getBase().name);
        record.setRoleId(role.getUid());
        record.setSrvId(role.getBase().srvId);
        record.setPlatformOrderId("IBD-"+GameKit.uid());
        record.setItemTid(itemRow.id);
        record.setItemNum(1);
        record.setItemName(itemRow.name);
        record.setItemNameCn(itemRow.nameCn);
        PayDeliverParam param = new PayDeliverParam();
        param.noVipPoints = cmd.isNoVipPoints();
        PayDeliverResult r = role.getPay().deliver(record, param);

        role.getBase().actor.payCentsTotal += itemRow.price;
        role.getBase().actor.payTimes++;
        cmd.setExeCode(0);

        String msg = "";
        msg += r.getBusinessComment();
        msg += " payCentsTotal=" + role.getBase().actor.payCentsTotal;
        msg += " payTimes=" + role.getBase().actor.payTimes;
        if(msg.length() > 199) {
            msg = msg.substring(0, 199);
        }
        cmd.setExeMsg(msg);
        return cmd;
    }

    /** ConsoleServer发起的托号调等级请求 */
    public static ActorLvLog lv(GameBody game, ActorLvLog cmd) {
        log.info("收到ConsoleServer发起的托号调等级请求：{}", cmd);
        Role role = game.role.getRole(cmd.getRoleId());
        syncData(cmd, role);
        int lvAdd = cmd.getLv() - role.getBase().level;
        if(lvAdd > 0) {
            role.getBase().levelUp(cmd.getLv() - role.getBase().level);
        }
        role.getGame().notice.message(role, "lvAdd="+lvAdd);
        cmd.setExeCode(0);
        cmd.setExeMsg("执行成功lvAdd="+lvAdd);
        return cmd;
    }

    /** ConsoleServer发起的托号备注请求 */
    public static StringRef note(GameBody game, ActorNoteCmd cmd) {
        log.info("收到ConsoleServer发起的托号备注请求：{}", cmd);
        Role role = game.role.getRole(cmd.getRoleId());
        role.getBase().actor.note = cmd.getNoteValue();
        return new StringRef("设置成功:"+cmd.getNoteValue());
    }

    /** ConsoleServer发起的托号更新数据请求 */
    public static ActorEntity write(GameBody game, ActorEntity entity) {
        log.info("收到ConsoleServer发起的托号更新数据请求：{}", entity);
        Role role = game.role.getRole(entity.getRoleId());
        entity.setGameId(role.getBase().gameId);
        entity.setGroupCode(role.getGame().config.gs.getGroupCode());
        entity.setSrvName(role.getGame().config.gs.getName());
        entity.setRoleName(role.getBase().name);
        entity.setOpenId(role.getBase().openId);
        entity.setPayCentsTotal(role.getBase().actor.payCentsTotal);
        entity.setPayTimes(role.getBase().actor.payTimes);
        entity.setEnabled(role.getBase().actor.enabled);
        entity.setNote(role.getBase().actor.note);
        return entity;
    }
}
